import pymel.core as pm

def lockAndHideAttr (nodeName,AttrList=(),all=False):
    '''
    Lock and hide atributes if they are not connected to something
    
    param :
    nodeName
    attrList = tuple list of attributes to lock and hide
    all : hide all attribute (boolean)
    '''
    nodeName = pm.PyNode(nodeName)
    if all :
        AttrList=[attr.split('.')[1] for attr in nodeName.listAttr(keyable=True)]
    for attr in AttrList :
        if not pm.connectionInfo(nodeName.attr(attr), isDestination=True) :
            if attr != 'visibility' :
                nodeName.attr(attr).setKeyable(False)
                nodeName.attr(attr).lock()
            else :
                nodeName.attr(attr).setKeyable(False)
                nodeName.attr(attr).showInChannelBox(False)
                
                
def snap (object1=None,object2=None,translation=True,rotation=True):
    '''
    Snap object1 to object2.
    
    Param :
    object 1
    object 2
    translation = boolean snap translation
    rotation = boolean snap rotation
    '''
    if not object1 :
        object1 = pm.selected()[0]
    if not object2 :
        object2 = pm.selected()[1]
    
    if translation :
        pm.delete(pm.pointConstraint(object2,object1,weight=1))
    if rotation :
        pm.delete(pm.orientConstraint(object2,object1,weight=1))

      
# flatten list
def iterableNotString (obj):
    '''
    Test if obj is iterable and not a string and return the result.
    '''
    try:
        iter(obj)
    except TypeError:
        return False
    else:
        return not isinstance(obj, basestring)
    
def listOrTuple (obj):
    '''
    Test if obj is a list or a tuple
    '''
    return isinstance(obj,(list,tuple))
    
def flattenList (sequence, toFlat=listOrTuple):
    '''
    Flatten a list.
    
    ex :
    >>> for elem in utils.flattenList ([1,2,3,[4,5,6,[7,8],9],10]) :
    >>> print elem,
    >>> 1,2,3,4,5,6,7,8,9,10
    '''
    for elem in sequence :
        if toFlat (elem) :
            for underElem in flattenList(elem,toFlat) :
                yield underElem
        else :
            yield elem